package com.xyh.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyh.common.util.ResultCode;
import com.xyh.common.util.ResultVO;
import com.xyh.system.mapper.RoleMenuMapper;
import com.xyh.system.mapper.RoleOperatorMapper;
import com.xyh.system.mapper.UserRoleMapper;
import com.xyh.system.model.Role;
import com.xyh.system.model.RoleMenu;
import com.xyh.system.model.RoleOperator;
import com.xyh.system.model.UserRole;
import com.xyh.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kyle
 * @since 2021-01-05
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleOperatorMapper roleOperatorMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    /**
     * url:role/index
     * 跳转至role-list界面
     *
     * @return
     */
    @GetMapping("/index")
    public String index() {
        return "role/role-list";
    }

    /**
     * url:role/list
     * 根据角色查询条件并分页获取所有角色列表
     *
     * @param page
     * @param limit
     * @param roleQuery
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultVO getList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                            Role roleQuery) {
        Page<Role> rolePage = roleService.selectAllByQuery(page, limit, roleQuery);
        return ResultVO.success(rolePage);
    }

    /**
     * 跳转至新增界面
     *
     * @return
     */
    @GetMapping
    public String toAdd() {

        return "role/role-add";
    }

    /**
     * 执行新增操作
     *
     * @param role
     * @return
     */
    @PostMapping
    @ResponseBody
    public ResultVO add(Role role) {

        boolean save = roleService.save(role);

        if (save) {
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

    /**
     * 跳转至更新页面
     *
     * @return
     */
    @GetMapping("/{roleId}")
    public String toUpdate(@PathVariable("roleId") Integer roleId, Model model) {

        model.addAttribute("role", roleService.getById(roleId));

        return "role/role-add";
    }

    /**
     * 执行更新操作
     *
     * @param role
     * @return
     */
    @PutMapping
    @ResponseBody
    public ResultVO update(Role role) {

        boolean b = roleService.updateById(role);

        if (b) {
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

    /**
     * 执行删除操作
     *
     * @param roleId
     * @return
     */
    @DeleteMapping("/{roleId}")
    @ResponseBody
    public ResultVO delete(@PathVariable("roleId") Integer roleId) {

        boolean b = roleService.removeById(roleId);
        if (b) {

            userRoleMapper.delete(new QueryWrapper<UserRole>().eq("role_id", roleId));
            roleMenuMapper.delete(new QueryWrapper<RoleMenu>().eq("role_id", roleId));
            roleOperatorMapper.delete(new QueryWrapper<RoleOperator>().eq("role_id", roleId));

            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

    /**
     * 通过角色id获取角色拥有的操作菜单权限
     *
     * @param roleId
     * @return
     */
    @GetMapping("/{roleId}/own/menu")
    @ResponseBody
    public ResultVO getRoleOwnMenu(@PathVariable("roleId") Integer roleId) {

        return ResultVO.success(roleService.getMenusByRoleId(roleId));
    }

    /**
     * 通过角色id，为该角色授予菜单权限
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("/{roleId}/grant/menu")
    @ResponseBody
    public ResultVO grantMenu(@PathVariable("roleId") Integer roleId, @RequestParam(value = "menuIds[]", required = false) Integer[] menuIds) {

        roleService.grantMenu(roleId, menuIds);
        return ResultVO.success();
    }

    /**
     * 通过角色id，获取角色拥有的操作菜单权限下的具体操作权限
     *
     * @param roleId
     * @return
     */
    @GetMapping("/{roleId}/own/operator")
    @ResponseBody
    public ResultVO getRoleOwnOperator(@PathVariable("roleId") Integer roleId) {
        Integer[] operatorIds = roleService.getOperatorsByRoleId(roleId);
        for (int i = 0; i < operatorIds.length; i++) {
            operatorIds[i] = operatorIds[i] + 10000;
        }
        return ResultVO.success(operatorIds);
    }

    /**
     * 通过角色id，为该角色授予功能权限
     *
     * @param roleId
     * @param operatorIds
     * @return
     */
    @PostMapping("/{roleId}/grant/operator")
    @ResponseBody
    public ResultVO grantOperator(@PathVariable("roleId") Integer roleId, @RequestParam(value = "operatorIds[]", required = false) Integer[] operatorIds) {
        roleService.grantOperator(roleId, operatorIds);
        return ResultVO.success();
    }
}
